package com.sdkj.recruit.controller;


import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.sdkj.recruit.common.Constants;
import com.sdkj.recruit.service.IMerchantManagementService;
import com.sdkj.recruit.service.IUserService;
import com.sdkj.recruit.util.DESEncryption;
import com.sdkj.recruit.util.MD5Util;
import com.sdkj.recruit.vo.MerchantLogVo;
import com.sdkj.recruit.vo.MerchantStatisticVo;
import com.sdkj.recruit.vo.MerchantTaskVo;
import com.sdkj.recruit.vo.MerchantVo;
import com.sdkj.recruit.vo.SimpleModelVo;
import com.sdkj.recruit.vo.UserVo;

import net.sf.json.JSONObject;



/**
 * 商户控制
 * @author chenxiaozhong
 *
 */
@Controller
@Path("/merchant")
public class MerchantManagementController {


	@Autowired
	private IUserService userService;

	@Autowired
	private IMerchantManagementService merchantService;
	
	private static final Logger logger = Logger.getLogger(MerchantManagementController.class);

	@GET
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Path("/queryMerchantList")
	public SimpleModelVo queryMerchantList(
			@QueryParam("currentPage") final int currentPage,
			@QueryParam("pageSize") final int pageSize,
			@QueryParam("merchantKey") final String merchantKey,
			@Context final HttpServletRequest request) {
		
		SimpleModelVo vo = new SimpleModelVo();
		List<MerchantStatisticVo> list = merchantService.queryMerchantList(currentPage,pageSize,merchantKey);
		long count =merchantService.queryMerchantCount(currentPage,pageSize,merchantKey);
		vo.setCount(count);
		vo.setList(list);
		return vo;
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Path("/updatePassword")
	public JSONObject updatePassword(@QueryParam("userId") final String userId,
			@QueryParam("oldpassword") final String oldpassword,
			@QueryParam("newpassword") final String newpassword,
			@Context final HttpServletRequest request) {

		UserVo userVo = userService.queryUserById(userId);

		JSONObject result = new JSONObject();

		if (userVo == null) {
			result.put("code", "-1");
			result.put("message", "用户不存在");
			return result;
		}

		String decryptStr = "";
		try {
			decryptStr = DESEncryption.decrypt(oldpassword,
					userVo.getUserName());
		} catch (IOException e) {
			logger.error("Error in decrypt. Reason: " + e.getMessage());
			result.put("code", "-1");
			result.put("message", "修改密码失败");
			return result;
		} catch (Exception e) {
			result.put("code", "-1");
			result.put("message", "修改密码失败");
			return result;
		}

		String createpassword = MD5Util.string2MD5(userVo.getUserName()
				.toLowerCase()
				+ Constants.USER_KEY
				+ MD5Util.string2MD5(decryptStr));

		if (!createpassword.equals(userVo.getPassword())) {
			result.put("code", "-1");
			result.put("message", "原始密码不正确");
			return result;
		}

		String newdecryptStr = "";
		try {
			newdecryptStr = DESEncryption.decrypt(newpassword,
					userVo.getUserName());
		} catch (IOException e) {
			logger.error("Error in decrypt. Reason: " + e.getMessage());
			result.put("code", "-1");
			result.put("message", "修改密码失败");
			return result;
		} catch (Exception e) {
			logger.error("Error in decrypt. Reason: " + e.getMessage());
			result.put("code", "-1");
			result.put("message", "修改密码失败");
			return result;
		}

		String createNewpassword = MD5Util.string2MD5(userVo.getUserName()
				.toLowerCase()
				+ Constants.USER_KEY
				+ MD5Util.string2MD5(newdecryptStr));

		userService.updatePassword(userId, createNewpassword);
		result.put("code", "0");
		result.put("message", "修改密码成功");
		return result;
	}
	
	/**
	 * 保存商家任务
	 * @param merchantTaskVo
	 * @param request
	 * @return
	 */
	@POST
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Path("/merchantTask")
	public boolean saveMerchantTask(final MerchantTaskVo merchantTaskVo, @Context final HttpServletRequest request) {
		
		return merchantService.saveMerchantTask(merchantTaskVo);
	}
	
	/**
	 * 商家
	 * @param merchantVo
	 * @param request
	 * @return
	 */
	@POST
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Path("/saveMerchant")
	public boolean saveMerchant(final MerchantVo merchantVo, @Context final HttpServletRequest request) {
		return merchantService.saveMerchant(merchantVo);
	}
	
	
	/**
	 * 商家登陆日志
	 * @param merchantVo
	 * @param request
	 * @return
	 */
	@POST
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Path("/saveMerchantLog")
	public boolean saveMerchantLog(final MerchantLogVo merchantLogVo, @Context final HttpServletRequest request) {

		return merchantService.saveMerchantLog(merchantLogVo);
	}
	
}
